package com.medical.admin.hospital.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.medical.admin.hospital.common.dto.ComboTypeDto;
import com.medical.admin.hospital.common.vo.ComboTypeVo;
import com.medical.admin.hospital.common.vo.PageVo;
import com.medical.admin.hospital.entity.ComboType;
import com.medical.admin.hospital.mapper.ComboTypeMapper;
import com.medical.admin.hospital.service.ComboTypeService;
import com.qf.medical.ex.BaseException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ComboTypeServiceImpl implements ComboTypeService {
    @Resource
    private ComboTypeMapper comboTypeMapper;

    @Resource
    private ObjectMapper objectMapper;

    /**
     * 新增套餐类型
     * @param comboTypeDto
     * @return
     */
    @Override
    public Integer insert(ComboTypeDto comboTypeDto) throws JsonProcessingException {
        ComboType comboType = findComboType(comboTypeDto);
        //判断数据是否存在
        if (ObjectUtil.isNotEmpty(comboType)){
            throw new BaseException(ResponseCode.HOSPITAL_EXIST_ERROR);
        }
        //对象转换String
        ComboType ct = BeanConvertUtils.convertTo(comboTypeDto, ComboType::new);
        ct.setComboInformation(objectMapper.writeValueAsString(comboTypeDto.getComboInformation().stream().map(combo->combo.getComboName()).collect(Collectors.toSet())));
        int count = comboTypeMapper.insert(ct);
        if (count!=1){
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
        return count;
    }



    /**
     * 修改套餐类型
     * @param comboTypeDto
     * @return
     */
    @Override
    public Integer update(ComboTypeDto comboTypeDto) {
        ComboType comboType= comboTypeMapper.selectById(comboTypeDto.getComboTypeId());
        Optional.ofNullable(comboType).orElseThrow(()->new BaseException(ResponseCode.COMBO_TYPE_NOT_EXIST_ERROR));
        //判断数据是否被删除
        if (comboType.getState()!=1){
            throw new BaseException(ResponseCode.COMBO_TYPE_NOT_EXIST_ERROR);
        }
        //对象转String
        comboType = BeanConvertUtils.convertTo(comboTypeDto, ComboType::new);
        try {
            comboType.setComboInformation(objectMapper.writeValueAsString(comboTypeDto.getComboTypeIntroduction()));
        } catch (JsonProcessingException e) {
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
        return comboTypeMapper.updateById(BeanConvertUtils.convertTo(comboTypeDto,ComboType::new));
    }

    /**
     * 删除套餐类型
     * @param comboTypeId
     * @return
     */
    @Override
    public Integer delete(Long comboTypeId) {
        ComboType comboType = comboTypeMapper.selectById(comboTypeId);
        Optional.ofNullable(comboType).orElseThrow(()->new BaseException(ResponseCode.COMBO_TYPE_NOT_EXIST_ERROR));
        comboType.setState(0);
        return comboTypeMapper.updateById(comboType);
    }

    /**
     * 查询套餐类型详情
     * @param comboTypeId
     * @return
     */
    @Override
    public ComboTypeVo select(Long comboTypeId) {
        ComboType comboType = comboTypeMapper.selectById(comboTypeId);
        if (ObjectUtil.isEmpty(comboType)){
            throw new BaseException(ResponseCode.COMBO_TYPE_NOT_EXIST_ERROR);
        }
        //判断数据是否被删除
        if (comboType.getState()!=1){
            throw new BaseException(ResponseCode.COMBO_TYPE_NOT_EXIST_ERROR);
        }
        return   BeanConvertUtils.convertTo(comboType, ComboTypeVo::new);
    }

    /**
     * 动态分页查询套餐类型
     * @param comboTypeName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVo<ComboTypeVo> list(String comboTypeName, int pageNum, int pageSize) {
        Page<ComboType> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ComboType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(comboTypeName!=null,ComboType::getComboTypeName,comboTypeName);
        Page<ComboType> comboTypePage = comboTypeMapper.selectPage(page, queryWrapper);
        //判断数据是否为空
        if (ObjectUtil.isEmpty(comboTypePage.getRecords())){
            throw new BaseException(ResponseCode.COMBO_TYPE_NOT_EXIST_ERROR);
        }
        PageVo<ComboTypeVo> comboTypes = new PageVo<>();

        comboTypes.setRows(BeanConvertUtils.convertListTo(comboTypePage.getRecords(),ComboTypeVo::new));
        comboTypes.setTotal(comboTypePage.getRecords().size());
        return comboTypes;
    }

    /**
     * 通过名称查询套餐信息
     * @param comboTypeDto
     * @return
     */
    private ComboType findComboType(ComboTypeDto comboTypeDto) {
        LambdaQueryWrapper<ComboType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(comboTypeDto.getComboTypeName()!=null,ComboType::getComboTypeName,comboTypeDto.getComboTypeName());
        return comboTypeMapper.selectOne(queryWrapper);
    }
}
